package com.zzw.entity;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 赵志伟
 * @version 1.0
 */
@SuppressWarnings({"all"})
//1.在类上自定义泛型
//2.静态成员中不能使用类的泛型
//3.Result<T>  是后端返回给前端的数据结构, 以json格式返回
//4.如果Result<T> 中的T是User, 那么返回的json数据就是User
public class Result<T> {
    private Integer code;//状态码
    private String msg;//状态说明
    private T data;//返回时，携带的数据
    private Map<String, Object> extend = new HashMap<>();//扩展参数

    public Result() {}

    //构造器, 指定返回的data
    public Result(T data) {
        this.data = data;
    }
    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    //编写方法, 返回需要的Result对象-表示成功, 不携带数据
    public static Result success() {
        Result result = new Result<>();
        result.setCode(200);
        result.setMsg("success");
        return result;
    }

    //编写方法, 返回需要的Result对象-返回成功的消息, 需要携带数据
    public static <T> Result<T> success(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    //编写方法, 返回需要的Result对象-返回成功的消息, 需要携带数据
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMsg("success");
        result.setData(data);
        return result;
    }

    //编写方法, 返回需要的Result对象-返回成功的消息, 需要携带数据
    public static <T> Result<T> success(T data, String msg) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static Result success(Boolean is) {
        return is ? Result.success(): Result.error();
    }

    public static <T> Result error() {
        Result<T> result = new Result<>();
        result.setCode(400);
        result.setMsg("error");
        return result;
    }

    public static <T> Result error(String msg) {
        Result<T> result = new Result<>();
        result.setCode(400);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result error(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> error(T data) {
        Result<T> result = new Result<>();
        result.setCode(400);
        result.setMsg("error");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(T data, String msg) {
        Result<T> result = new Result<>(data);
        result.setCode(400);
        result.setMsg(msg);
        return result;
    }

    public Result add(String key, Object value) {
        extend.put(key, value);
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
